﻿using System;
using System.Collections.Generic;
using DataEntities;
using NUnit.Framework;
using Repositories.Repository;
using Repositories.UoW;
namespace TestRepositories
{
    [TestFixture]
    public class TestHistoryCommonReceiptsDataRepository : ITestRepository
    {
        private IHistoryCommonReceiptsDataRepository _repository;
        private RepositoryFactory _repositoryFactory;
        private HistoryCommonReceiptsData _historyCommonReceiptsData;
        private IDogovorRepository _dogovorRepository;

        [SetUp]
        public void SetUp()
        {
            SessionFactory.Init();
            SessionFactory.GetSession().Flush();
            _repositoryFactory = new RepositoryFactory();
            _repository = _repositoryFactory.GetHistoryCommonReceiptsDataRepository();
            _dogovorRepository = _repositoryFactory.GetDogovorRepository();

            _historyCommonReceiptsData = new HistoryCommonReceiptsData(new DateTime(2011, 01, 01), 1000, 1000, _dogovorRepository.Get(3));

        }

        [TearDown]
        public void CleanUp()
        {
        }

        [Test]
        //[Ignore]
        public void TestGet()
        {
            Assert.AreEqual(23, _repository.Get(23).Id);
            Assert.AreEqual(34, _repository.Get(28).Dogovor.Id);
        }

        [Test]
        [Ignore]
        public void TestGetList()
        {
            Assert.AreEqual(8205, _repository.GetList().Count);
        }

        [Test]
        //[Ignore]
        public void TestInsert()
        {
            Assert.AreEqual(1, _repository.Insert(_historyCommonReceiptsData));
            Assert.AreEqual(1000, _repository.Get(_historyCommonReceiptsData.Id).Rcount);
        }

        [Test]
        //[Ignore]
        public void TestUpdate()
        {
            _historyCommonReceiptsData = _repository.Get(238);
            _historyCommonReceiptsData.Rsumma += 1000;
            Assert.AreEqual(1, _repository.Update(_historyCommonReceiptsData));
            Assert.AreEqual(2520, _repository.Get(238).Rsumma);
        }

        [Test]
        [Ignore]
        public void TestDelete()
        {
            _historyCommonReceiptsData = new HistoryCommonReceiptsData(4, new DateTime(2007, 7, 1), 0, 0,
                                                                       _dogovorRepository.Get(6));
            Assert.AreEqual(1, _repository.Delete(_historyCommonReceiptsData));
        }

        [Test]
        //[Ignore]
        public void TestEquals()
        {
            _historyCommonReceiptsData = new HistoryCommonReceiptsData(239, new DateTime(2007, 08, 01), 36, 1520, _dogovorRepository.Get(72));
            Assert.AreEqual(_historyCommonReceiptsData, _repository.Get(239));
        }

        [Test]
        //[Ignore]
        public void TestInsertList()
        {
            IList<HistoryCommonReceiptsData> list = new List<HistoryCommonReceiptsData>();
            list.Add(new HistoryCommonReceiptsData(new DateTime(2011, 01, 01), 10000, 10000, _dogovorRepository.Get(3)));
            list.Add(new HistoryCommonReceiptsData(new DateTime(2011, 01, 01), 10000, 10000, _dogovorRepository.Get(3)));
            list.Add(new HistoryCommonReceiptsData(new DateTime(2011, 01, 01), 10000, 10000, _dogovorRepository.Get(3)));
            Assert.AreEqual(3, _repository.InsertList(list));
        }

        [Test]
        //[Ignore]
        public void UpdateList()
        {
            IList<HistoryCommonReceiptsData> list = new List<HistoryCommonReceiptsData>();
            list.Add(_repository.Get(50));
            list.Add(_repository.Get(4));
            list.Add(_repository.Get(6));
            list.Add(_repository.Get(8));

            foreach (HistoryCommonReceiptsData item in list)
            {
                item.Rcount += 1000;
                item.Rsumma += 1000;
            }

            Assert.AreEqual(4, _repository.UpdateList(list));
        }

        [Test]
        [Ignore]
        public void TestDeleteList()
        {
            IList<HistoryCommonReceiptsData> list = new List<HistoryCommonReceiptsData>();
            list.Add(new HistoryCommonReceiptsData(2, new DateTime(2007, 7, 1), 0, 0, _dogovorRepository.Get(3)));
            list.Add(new HistoryCommonReceiptsData(3, new DateTime(2007, 7, 1), 0, 0, _dogovorRepository.Get(5)));
            list.Add(new HistoryCommonReceiptsData(69, new DateTime(2007, 7, 1), 0, 0, _dogovorRepository.Get(78)));
            list.Add(new HistoryCommonReceiptsData(5, new DateTime(2007, 7, 1), 0, 0, _dogovorRepository.Get(7)));
            Assert.AreEqual(4, _repository.DeleteList(list));
        }
    }
}